﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Collections;
using System.Windows.Forms;

namespace MassIntellect.WorkIt.Design
{
    /// <summary>
    /// Редактор коллекции
    /// </summary>
    /// <typeparam name="Tcomp">Совместимый тип (интерфейс)</typeparam>    
    public class CollectionEditor<T> : UITypeEditor
        where T:class
    {
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService svc =
               (IWindowsFormsEditorService)
               provider.GetService(typeof(IWindowsFormsEditorService));

            if (svc != null)
            {
                using (frmCollectionEditor<T> ipfrm = new frmCollectionEditor<T>((ICollection<T>)value))
                    if (svc.ShowDialog(ipfrm) == DialogResult.OK)
                        value = ipfrm.Collection;
            }
            return base.EditValue(context, provider, value);
        }        

        /// <summary>
        /// Возвращаем стиль редактора - модальное окно
        /// </summary>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null)
                return UITypeEditorEditStyle.Modal;
            else
                return base.GetEditStyle(context);
        }
    }
}
